package demo4;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-08-09
 * Time: 18:13
 */
public class Test {
    private static String message = "A";
    private static Object lock = new Object();

    public static void main(String[] args) {

        new Thread(() -> {
            int count = 1;
            synchronized (lock) {
                while (count < 101) {

                    while (!message.equals("A")) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(message);
                    message = "B";
                    count = count + 1;
                    lock.notifyAll();
                }
            }

        },"A").start();

        new Thread(() -> {
            int count = 1;
            synchronized (lock) {
                while (count < 101) {
                    while (!message.equals("B")) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(message);
                    message = "C";
                    count = count + 1;
                    lock.notifyAll();
                }
            }

        },"B").start();
        new Thread(() -> {
            int count = 1;
            synchronized (lock) {
                while (count < 101) {
                    while (!message.equals("C")) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(message);
                    message = "A";
                    count = count + 1;
                    lock.notifyAll();
                }
            }

        },"C").start();


    }
}

